<?php echo form_tag('bom/save', array(
  'id'        => 'sf_admin_edit_form',
  'name'      => 'sf_admin_edit_form',
  'multipart' => true,
)) ?>

<?php echo object_input_hidden_tag($bom, 'getId')?>

<fieldset id="sf_fieldset_none" class="">
<?php 
    if ($bom->getReviseId()) { ?>
        <input type="hidden" name="bom[revise_id]" id="bom_revise_id" value="<?php echo $bom->getReviseId() ?>" >
<?}        
?>
<div class="form-row">
  <?php echo label_for('bom[wono]', __($labels['bom{wono}']), 'class="required" ') ?>
  <div class="content<?php if ($sf_request->hasError('bom{wono}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('bom{wono}')): ?>
    <?php echo form_error('bom{wono}', array('class' => 'form-error-msg')) ?>
  <?php endif; ?>

<!--Tien-->
  <?php $default = CommonValues::GenerateAutoNumber('BOM', $bom->getWono(), 'BOM')   ?>
   
   <input name="bom_wono_show" id="bom_wono_show" value="<?php echo $default ?>" size="15" disabled="disabled" type="text">
   </div>
</div>

<div class="form-row">
  <?php echo label_for('bom[project_id]', __($labels['bom{project_id}']), 'class="required" ') ?>
  <div class="content<?php if ($sf_request->hasError('bom{project_id}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('bom{project_id}')): ?>
    <?php echo form_error('bom{project_id}', array('class' => 'form-error-msg')) ?>
  <?php endif; ?>

  <?php $value = object_select_tag($bom, 'getProjectId', array (
  'related_class' => 'Project',
  'control_name' => 'bom[project_id]',
  'peer_method'=>'getSorted',
));
  
    if ($bom->getReviseId())  {?>     
   <input name="bom[project_id]" id="bom_project_id" value="<?php echo$bom->getProjectId() ?>"  type="hidden">
   <input name="bom_wono_show" id="bom_wono_show" value="<?php echo $bom->getProject() ?>" size="30" disabled="disabled" type="text">
    <?}
    else 
        echo $value ? $value : '&nbsp;' ?>
    </div>
</div>
 <div class="form-row">
  <?php echo label_for('bom[created_at]', __($labels['bom{created_at}']), '') ?>
  <div class="content<?php if ($sf_request->hasError('bom{created_at}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('bom{created_at}')): ?>
    <?php echo form_error('bom{created_at}', array('class' => 'form-error-msg')) ?>
  <?php endif; ?>

  <?php $value = object_input_date_tag($bom, 'getCreatedAt', array (
  'rich' => true,
  'calendar_button_img' => '/sf/sf_admin/images/date.png',
  'control_name' => 'bom[created_at]',
)); echo $value ? $value : '&nbsp;' ?>
    </div>
</div>



<div class="form-row">
  <?php echo label_for('bom[status]', __($labels['bom{status}']), '') ?>
  <div class="content<?php if ($sf_request->hasError('bom{status}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('bom{status}')): ?>
    <?php echo form_error('bom{status}', array('class' => 'form-error-msg')) ?>
  <?php endif; ?>

  <?php 
        $status = $bom->getStatus()? $bom->getStatus(): CommonValues::$gBomStatus['key']['Initial'];
        $gStatus = CommonValues::$gBomStatus['value']; 
   ?>
    <input name="bom[status]" id="bom_status" value="<?php echo $status ?>"  type="hidden">
    <input name="bom_status_show" id="bom_status_show" value="<?php echo $gStatus[$status] ?>" size="10" disabled="disabled" type="text">
    </div>
</div>
        <input name="bom[approvalstatus]" id="bom_approvalstatus" value="<?php echo $bom->getApprovalstatus() ?>"  type="hidden">
 
</fieldset>

<!--Tien code here
get information for MR-->
<?php
    if ($bom->getComponentgroupId()){
        $componentGroupId = $bom->getComponentgroupId();
        
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID , $componentGroupId);
        $c->addAscendingOrderByColumn(ComponentsPeer::COMPONENT_ID);
        
        $componentList = ComponentsPeer::doSelect($c);
    }
?>


<?php 
echo link_to_remote('', array('update' => 'list_component', 
  'url' => 'component/listAjax', 
  'loading' => visual_effect('appear', 'indicator'),
  'complete' => visual_effect('fade', 'indicator'). visual_effect('highlight', 'list_component'),
  'position' => 'bottom',
));
?>

<div id="sf_admin_bar">

<div class="sf_admin_filters">

  <fieldset>
    <h2>Filters</h2>
    
    <div class="form-row">
    <label for="filters_alert_date">Category:</label>
    <div class="content">
        <select name="category" id="category" class="category">
            <?php foreach (CommonValues::$gCompCategory['key'] as $key=>$category) 
            if ($category != CommonValues::$gCompCategory['key']['Raw Material'] ){ ?>
            <option value="<?php echo $category; ?>"> <?php echo $key ?></option>
            <?php } ?>
        </select>
    </div>
    </div>

    <div class="form-row">
    <label for="filters_alert_date">Description:</label>
    <div class="content">
        <input type="text" name="description" id="description" />
    </div>
    </div>
    
    <div class="form-row">
    <label for="filters_alert_date">Product Code:</label>
    <div class="content">
        <input type="text" name="productcode" id="productcode" />
    </div>
    </div>

   </fieldset>

	  <ul class="sf_admin_actions">    
	    <li><input type="button" name="filter" value="Filter" class="sf_admin_action_filter"  onclick="changeList()" /></li>
	  </ul>
	  
    <div id="indicator" style="display: none">Loading...</div>

    
</div>
</div>  	    
       <div id ="list_component">    
          <table id ='ListOfComponents' "cellspacing="0" class="sf_admin_list">            
            <thead>
            <tr>
                  <th id="sf_admin_list_th_productcode" width="25%">Product Code
                          </th>
                  <th id="sf_admin_list_th_description" width="35%">Description
                          </th>
                  <th id="sf_admin_list_th_description" width="20%">Brand
                          </th>       
                  <th id="sf_admin_list_th_description" width="15%">Unit
                          </th>
                  <th id="sf_admin_list_th_description" width="5%">Add
                          </th>
            </tr>
            </thead>            
          </table>
       </div>
            
           
    
    <ul class="sf_admin_actions">    
	    <li><input type="button" name="Add Components" value="Add Components" class="sf_admin_action_filter"  onclick="addComponents()" /></li>	    
    </ul>

<table id ='added_components' "cellspacing="0" class="sf_admin_list">
<thead>
<tr>
      <th id="sf_admin_list_th_productcode" width="25%">Product Code
              </th>
      <th id="sf_admin_list_th_description" width="35%">Description
              </th>
      <th id="sf_admin_list_th_description" width="10%">Brand
              </th>       
      <th id="sf_admin_list_th_description" width="5%">Unit
              </th>
      <th id="sf_admin_list_th_description" width="5%">Exp Dlv Date (dd-mm-yy)</th>
      <th id="sf_admin_list_th_description">Qty need</th>
      <th id="sf_admin_list_th_description">Supplied by Client</th>
      
      <th id="sf_admin_list_th_description" width="5%">Action
              </th>
</tr>
</thead>
<!--show component here-->
 <?php if (isset($bom->rowIds) && count($bom->rowIds)) {
    foreach ($bom->rowIds as $key=>$id) { 
        $component1= ComponentPeer::retrieveByPK($id);?>
        <tr class="sf_admin_row_<?php echo $key%2?>">
            <input type="hidden" name="rowIds[]" id ="rowIds"  class = "ExistComponents" value="<? echo $component1->getId()?>"/>
            <td><?echo $component1->getProductcode()?></td>
            <td><?echo $component1->getDescription()?></td>
            <td><?echo $component1->getBrand()?></td>
            <td><?echo $component1->getComponentunit()?></td>
            <td><input type="text" name="expdates[]" id ="expdates" size="8" value ="<?echo $bom->expdates[$key]?>"></td>
            <td><input type="text" name="qtys[]" id ="qtys" size="5" value ="<?echo $bom->qtys[$key]?>"></td>            
            <td><input type="checkbox" name="suppliedByClients[]" id ="suppliedByClients"  value="<?php echo $component1->getId();?>"        
        <?php 
            $bSupplied = false;
            for ($j = 0; isset($bom->suppliedByClients) && $j < count($bom->suppliedByClients) ; $j++) {
                if ($bom->suppliedByClients[$j] == $id) {
                    $bSupplied = true;
                    break;
                }
            }
            if ($bSupplied) echo "checked";
        ?>></td>
            <td><ul class="sf_admin_td_actions"><li><?php echo  image_tag(sfConfig::get('sf_admin_web_dir').'/images/delete_icon.png', array('align' => 'absmiddle', 'alt' => 'delete', 'title' =>'delete', 'onclick' => 'removeComponent('.$component1->getId().',"added_components", ".ExistComponents")', 'onmouseover'=>'this.style.cursor="pointer"'))?></li></ul></td>
        </tr>
<?} } elseif (isset($componentList) && $componentList) {
    foreach ($componentList as $key=>$components) { 
        $component1= ComponentPeer::retrieveByPK($components->getComponentId());?>
        <tr class="sf_admin_row_<?php echo $key%2?>">
            <input type="hidden" name="rowIds[]" id ="rowIds"  class = "ExistComponents" value="<? echo $component1->getId()?>"/>
            <td><?echo $component1->getProductcode()?></td>
            <td><?echo $component1->getDescription()?></td>
            <td><?echo $component1->getBrand()?></td>
            <td><?echo $component1->getComponentunit()?></td>
            <td><input type="text" name="expdates[]" id ="expdates" size="8" value ="<?echo $components->getDate('d-m-Y')?>"></td>
            <td><input type="text" name="qtys[]" id ="qtys" size="5" value ="<?echo $components->getQty1()?>"></td>            
            <td><input type="checkbox" name="suppliedByClients[]" id ="suppliedByClients"  value="<?php echo $components->getComponentId();?>"  <?php if (1 ==$components->getQty2()) echo "checked"?>></td>
            <td><ul class="sf_admin_td_actions"><li><?php echo  image_tag(sfConfig::get('sf_admin_web_dir').'/images/delete_icon.png', array('align' => 'absmiddle', 'alt' => 'delete', 'title' =>'delete', 'onclick' => 'removeComponent('.$component1->getId().',"added_components", ".ExistComponents")', 'onmouseover'=>'this.style.cursor="pointer"'))?></li></ul></td>
        </tr>
<?php } }?>
</table>


<?php include_partial('edit_actions', array('bom' => $bom)) ?>

</form>

<ul class="sf_admin_actions">
      <li class="float-left"><?php if ($bom->getId()): ?>
<?php echo button_to(__('delete'), 'bom/delete?id='.$bom->getId(), array (
  'post' => true,
  'confirm' => __('Are you sure?'),
  'class' => 'sf_admin_action_delete',
)) ?><?php endif; ?>
</li>
  </ul>

<script language="JavaScript">

function changeList(){
    var description=$("description").value;
    var productcode=$("productcode").value;
    var category = $("category").value;
    var link = '<?php echo url_for('component/listAjax') ?>';
    new Ajax.Updater('list_component', link + '?description='+ description + '&productcode=' + productcode + '&category=' + category, {asynchronous:true, evalScripts:false, onComplete:function(request, json){new Effect.Fade('indicator', {});new Effect.Highlight('list_component', {});}, onLoading:function(request, json){new Effect.Appear('indicator', {});}});  
    }
    
function addComponents()
{
    var components = $$(".ChkboxComponents");        
    var existComponents = $$(".ExistComponents");                
    var table = $("added_components");
    var count = table.rows.length;
    
    for (var i =0; i< components.length ; i++ ){
        if (components[i].checked){
            var component = components[i];            
            var rowId = component.attributes['rowId'].value;
            var bExist = false;
            for (var j =0; j< existComponents.length; j++)
                if (rowId== existComponents[j].value){
                    bExist = true;
                    break;
                }
            if (!bExist) {            
                var oddRow=(count+1) % 2;
                var productCode='<td>' + $("productCode_"+ rowId).value + '</td>';          
                var description='<td>'+ $("description_"+ rowId).value + '</td>';          
                var brand='<td>' + $("brand_"+ rowId ).value + '</td>';  
                var unit='<td>' + $("unit_"+ rowId ).value + '</td>';
                var rowIds = '<input type="hidden" name="rowIds[]" id ="rowIds"  class = "ExistComponents" value="' + rowId+ '"/>' ;                         
                var action = '<td><ul class="sf_admin_td_actions"><li>' + '<?php echo  image_tag(sfConfig::get('sf_admin_web_dir').'/images/delete_icon.png', array('align' => 'absmiddle', 'alt' => 'delete', 'title' =>'delete', 'onclick' => 'removeComponent(ROWID, "added_components", ".ExistComponents")', 'onmouseover'=>'this.style.cursor="pointer"') ); ?>' +  '</li></ul></td>';
                action = action.replace("ROWID", rowId);                
                
                var suppliedByClient = '<td><input type="checkbox" name="suppliedByClients[]" value="' + rowId+ '"  ></td>';
                var qty='<td><input type="text" name="qtys[]" id="qtys" size="5" /></td>';
                
                var expdate ='<td><input name="expdates[]" id="expdates" value="" size="8" type="text"></td>';
                
                var newRow = table.insertRow(count);
                newRow.className = 'sf_admin_row_'+ oddRow;
                newRow.innerHTML= rowIds +  productCode + description+ brand + unit + expdate + qty + suppliedByClient + action;
                ++count;
            }                       
        }
        
    }
    //remove component  row after add to exist
    var componentList = $("ListOfComponents");
    var j=0;
    for (var i =0; i< components.length ;  i++){
        if (components[i].checked){
            var component = components[i];            
            var rowId = component.attributes['rowId'].value;
             componentList.deleteRow(i+1 - j);
             j++;
        }
    }
    //change classname, begin from 1 because there is a row with thead title
    for (var i =1; i< componentList.rows.length; i++)    {
        componentList.rows[i].className='sf_admin_row_' + (i+1)%2;
    }
}

function removeComponent(rowId, tableId, classId){
    var existComponents = $$(classId);
    var components = $(tableId);
    
    for (var i =0; i < existComponents.length; i++){
        if (rowId == existComponents[i].value){
            components.deleteRow(i+1);
            break;
        }        
    }
    //because there is a row with thead title
    for (var i =1; i< components.rows.length; i++)    {
        components.rows[i].className='sf_admin_row_'+ (i+1)%2;
    }
}
</script>